package com.lws.basic.base;

import android.os.Bundle;

import android.view.View;

import io.reactivex.annotations.Nullable;


public abstract class BaseLazyFragment extends BaseFragment {
    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;
    @Override
    public abstract int setLayoutId();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    private void lazyLoad() {
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载

            isUIVisible = false;
        }
    }

    @Override
    public void onDestroy() {
        isViewCreated = false;
        super.onDestroy();
    }

    protected abstract void loadData();
}
